//
//  DDReportFile.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDReportFile.h"
#import "DDFileInfo.h"
#import "NSMutableData+Replace.h"
#import "NSString+md5.h"

@implementation DDReportFile

- (id)initWithData:(NSData *)data {
    self = [super initWithData:data];
    return self;
}

- (id)initWithData:(int)type fileCount:(int)count fileInfos:(NSArray *)infos{
    self = [super init];
    if (self) {
        [self setType:type];
        [self setFileCount:count];
        [self setFileInfos:infos];
        [self setCmd:CloudCmd_ReportFile];
        int length = HEADER_LENGTH + 4 + 4 +(MAX_PATH * 2 + 4 + 8)*([infos count]);
        [self setLength:length];
        int crc = [self crcs:entity withLength:length];
        [self setCrc:crc];
    }
    return self;
}

-(void) setType:(int)type{
    NSRange range;
    range.length = 4;
    range.location = HEADER_LENGTH;
    NSData* typeData = [[NSData alloc]initWithBytes:&type length:sizeof(type)];
    [entity replaceBytesInRange:range withBytes:[typeData bytes] ];
}

- (void)setFileCount:(int)fileCount{
    NSRange range;
    range.length = 4;
    range.location = HEADER_LENGTH + 4;
    NSData* countData = [[NSData alloc]initWithBytes:&fileCount length:sizeof(fileCount)];
    [entity replaceBytesInRange:range withBytes:[countData bytes]];
}

- (void)setFileInfos:(NSArray *)fileInfos{
    if (fileInfos == nil || [fileInfos count]==0) {
        return;
    }
    for (int i = 0; i < [fileInfos count]; i++) {
        DDFileInfo* info = [fileInfos objectAtIndex:i];
        NSRange range;
        /*-----------放入文件的ID----------------*/
        range.location = (HEADER_LENGTH + 4 + 4) + ((MAX_PATH * 2 + 4 + 8)*i);
        range.length = 4;
        int fileId = [info fileId];
        NSData* fileIdData = [[NSData alloc]initWithBytes:&fileId length:sizeof(fileId)];
        [entity replaceBytesInRange:range withBytes:[fileIdData bytes]];
        
        /*-----------放入文件的name----------------*/
        range.location = range.location + range.length;
        range.length = MAX_PATH * 2;
        NSString* fileName = [info fileName];
//        fileName = [NSString stringWithFormat:@"%@%c%c%c%c",fileName,0x00,0x00,0x00,0x00];
        
        NSLog(@"SEND----%@,%d",fileName,[fileName length]);
//        fileName = [NSString stringWithFormat:@"%@%C",fileName,'\0'];
        [entity replaceByString:fileName length:(MAX_PATH * 2) offset:range.location];
        /*-----------放入文件的length----------------*/
        range.location = range.location + range.length;
        range.length = 8;
        long fileLength = [info fileLength];
        NSData* lengthData = [NSData dataWithBytes:&fileLength length:sizeof(fileLength)];
        [entity replaceBytesInRange:range withBytes:[lengthData bytes]];
        
    }
}

- (int) getType{
    int type = -1;
    
    NSRange range;
    range.length = 4;
    range.location = HEADER_LENGTH;
    NSData* typeData = [entity subdataWithRange:range];
    type = [self dataToInt:typeData];
    return type;
}
-(int) getFileCount{
    int fileCount = -1;
    NSRange range;
    range.length = 4;
    range.location = HEADER_LENGTH + 4;
    NSData* fileCountData = [entity subdataWithRange:range];
    fileCount = [self dataToInt:fileCountData];
    return fileCount;
}

-(NSArray*) getFileInfos{
    int fileCount = [self getFileCount];
    NSMutableArray* fileInfos = [[NSMutableArray alloc]init];
    for (int i =0 ; i < fileCount; i++) {
        DDFileInfo* fileInfo = [[DDFileInfo alloc]init];
        /*-----------取出文件ID------------*/
        NSRange range;
        range.location = HEADER_LENGTH + 4 * 2 + MAX_PATH * 2* i + 4 * i + 8 * i;
        range.length = 4;
        NSData* idData = [entity subdataWithRange:range];
        int uid = [self dataToInt:idData];
        [fileInfo setFileId:uid];
        
        /*-----------取出文件名-------------*/
        range.location = range.location + 4;
        range.length = MAX_PATH * 2;
        NSData* nameData = [entity subdataWithRange:range];
        Byte bytes[] = {255,254};
        bytes[0] = 255;
        bytes[1] = 254;
        NSMutableData *headData = [[NSMutableData alloc]initWithBytes:bytes length:2];
        [headData appendData:nameData];
        NSString* name = [[NSString alloc] initWithData:headData encoding:NSUnicodeStringEncoding];
        //name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        name = [name trim00];
        [fileInfo setFileName:name];
        
        /*-----------取出文件长度------------*/
        range.location = range.location + MAX_PATH * 2;
        range.length = 8;
        NSData *lengthData = [entity subdataWithRange:range];
        long length = 0;
        [lengthData getBytes:&length length:LONG_SIZE];
        [fileInfo setFileLength:length];
        [fileInfos addObject:fileInfo];
    }
    return fileInfos;
}


@end
